/*
 * Copyright (c) 2008 Kevin Wetzels
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package be.roam.jindy.string;

import static java.lang.Character.isWhitespace;

/**
 * Utility methods for dealing with strings.
 * 
 * @author Kevin Wetzels
 */
public class StringUtils {

	/**
	 * Checks if the given string is empty or null.
	 * <p>
	 * Whitespace is ignored, which means <code>"  "</code> is empty, as is
	 * <code>" \n "</code>.
	 * </p>
	 * 
	 * @param string
	 *            string to check
	 * @return <code>true</code> if the string is <code>null</code> or consists
	 *         solely of whitespace characters.
	 */
	public static boolean isEmpty(String string) {
		if (string == null || string.length() == 0) {
			return true;
		}
		for (int index = 0; index < string.length(); ++index) {
			if (!isWhitespace(string.charAt(index))) {
				return false;
			}
		}
		return true;
	}

	/**
	 * Strips HTML or XML tags from the string, replacing the tags with a single
	 * space.
	 * 
	 * @param string
	 *            string to strip
	 * @return stripped string
	 */
	public static String stripTags(String string) {
		return stripTags(string, " ");
	}

	/**
	 * Strips HTML or XML tags from the string.
	 * 
	 * @param string
	 *            string to strip
	 * @param replacement
	 *            the replacement string for each tag
	 * @return the stripped string
	 */
	public static String stripTags(String string, String replacement) {
		if (isEmpty(string)) {
			return string;
		}
		return string.replaceAll("<[^>]+>", replacement);
	}

	/**
	 * Limits the string to the desired number of characters, adding "..." to
	 * the end when it's longer than that.
	 * 
	 * @param string
	 *            string to limit
	 * @param maximumLength
	 *            the maximum length of the string
	 * @return the string itself when it's smaller than the maximum length or
	 *         the substring of the string with "..." added to the back.
	 */
	public static String limit(String string, int maximumLength) {
		return limit(string, maximumLength, "...");
	}

	/**
	 * Limits the string to the desired number of characters, adding the suffix
	 * to the result.
	 * 
	 * @param string
	 *            string to limit
	 * @param maximumLength
	 *            the maximum length of the string
	 * @param suffix
	 *            the suffix to add to the string
	 * @return the string itself when it's less than the maximum number of
	 *         characters or the substring of the string, concatenated with the
	 *         suffix
	 */
	public static String limit(String string, int maximumLength, String suffix) {
		if (string == null) {
			return string;
		}
		int stringLength = string.length();
		if (stringLength <= maximumLength) {
			return string;
		}
		return string.substring(0, maximumLength) + (suffix == null ? "" : suffix);
	}

}
